package com.bootdo.activity;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 排他网关示例 {

        @Before
        public void testCreateTablesAutomaticallyWithoutConfigFiles() {
            // 创建流程引擎配置对象
            ProcessEngineConfiguration config = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
            // 设置jdbc连接参数信息
            config.setJdbcDriver("com.mysql.jdbc.Driver");
            config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/new_frame_dubbo?useUnicode=true&characterEncoding=utf8");
            config.setJdbcUsername("root");
            config.setJdbcPassword("root");
            // 设置自动建表
            config.setDatabaseSchemaUpdate("true");
            // 使用配置对象创建流程引擎对象，创建对象过程中会自动建表
            ProcessEngine processEngine = config.buildProcessEngine();
        }


        /**
         * 部署流程
         */
        @Test
        public void startDeployTest() {
            ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
            processEngine.getRepositoryService()
                    .createDeployment()
                    .name("请假流程：情况五")
                    .addClasspathResource("paita.bpmn")
                    .deploy();
        }
    /**
     * 启动流程实例
     *    可以设置一个流程变量
     */
    @Test
    public void testStartPI(){
        /**
         * 流程变量
         *   给<userTask id="请假申请" name="请假申请" activiti:assignee="#{student}"></userTask>
         *     的student赋值
         */
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "小明");
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
                .startProcessInstanceById("myProcess_1:3:67504",variables);
    }

    @Test
    public void testFinishTask_Teacher(){
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("user", "小明2");
        variables.put("day", 15);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
                .complete("77505", variables); //完成任务的同时设置流程变量
    }


    @Test
    public void testQueryTask(){
        //下面代码中的小毛，就是我们之前设计那个流程图中添加的班主任内容
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee("老毛")
                .list();

        for (Task task : tasks) {
            String user = (String)  processEngine.getTaskService().getVariable(task.getId(), "user");
            System.out.println(user);
            System.out.println(task.getId());
            System.out.println(task.getName());
        }
    }

}
